package com.itheima.d3_tcp.no2;

import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

//目标：掌握tcp客户端多发消息
public class ClientDemo02 {
    public static void main(String[] args) throws Exception {
        //1.发送的消息需要键盘录入得到
        Scanner sc = new Scanner(System.in);
        sc.useDelimiter("\n");

        //2.创建Socket对象，传入服务端的ip和端口，底层尝试三次握手
        Socket socket = new Socket("localhost", 10004);
        OutputStream os = socket.getOutputStream();
        PrintStream ps = new PrintStream(os);

        //3.定义死循环，不断发送
        while (true) {
            System.out.println("请输入：");
            String msg = sc.next();

            //4.判断msg是否是"exit"
            if ("exit".equals(msg)) {
                //结束循环
                break;
            }

            //5.如果代码可以执行到这，要把消息发出去
            ps.println(msg);
            ps.flush();
        }

        //释放资源
        socket.close();
    }
}
